package com.cdut.logistics.action.datacentre;

import java.util.ArrayList;
import java.util.List;

import com.cdut.logistics.intercepter.system.AuthorityIntercepter;
import com.cdut.logistics.model.dao.datacentre.Dict;
import com.cdut.logistics.service.impl.datacentre.DictImpl;
import com.cdut.logistics.service.interf.datacentre.DictService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**  
 * @ClassName: DictController  
 * @Description: 字典值管理控制类
 * @author wxt.xia
 * @date 2018年1月26日 下午3:28:25    
 */
//@Before(AuthorityIntercepter.class)
public class DictController extends Controller {
	
	private DictService dictService = new DictImpl();
	
	/**
	 * 
	 * @Title: queryById  
	 * @Description: 根据字典值id查询字典值
	 * @author wxt.xia
	 * @param json
	 * @throws
	 */
	public void queryById(){
		/*
		 * 解析json
		 */
		int id = Integer.valueOf((String) getAttr("dict_id"));
		Record dict = new Record();
		try {
			dict = dictService.queryById(id);
			System.out.println(dict.toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		renderJson(dict);
	}
	
	/**
	 * 
	 * @Title: query  
	 * @Description: 根据字典描述查询
	 * @author wxt.xia
	 * @param json
	 * @throws
	 */
	public void query(){
		/*
		 * 解析json
		 */
		String description = getAttr("description")+"%";
		List<Record> dictList = new ArrayList<Record>();
		try {
			dictList = dictService.query(description);
		} catch (Exception e) {
			// TODO: handle exception
		}
		renderJson(dictList);
	}
	
	/**
	 * 
	 * @Title: query  
	 * @Description: 根据字典值名称查询区域
	 * @author wxt.xia
	 * @param json
	 * @throws
	 */
	public void queryByType(){
		/*
		 * 解析json
		 */
		String type = getPara("type")+"%";
		List<Record> dictList = new ArrayList<Record>();
		try {
			dictList = Db.find(new Dict().sql5,type);
			System.out.println(dictList.get(0).toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		renderJson(dictList);
	}
	
	/**
	 * 
	 * @Title: add  
	 * @Description: 新增字典值
	 * @author wxt.xia
	 * @throws
	 */
	public void add(){
		/*
		 * 解析json
		 */
		
		//下面是测试数据，id需设置为自增长
		Dict dict = new Dict();
		dict.set("value", 2);
		dict.set("type", 0);
		dict.set("label", "维修类型");
		dict.set("description", "水龙头");
		dict.set("sort", 10);
		
		try {
			System.out.println("控制类*************");
			dictService.add(dict);
		} catch (Exception e) {
			
		}
	}
	
	/**
	 * 
	 * @Title: form  
	 * @Description: 修改字典值
	 * @author wxt.xia
	 * @throws
	 */
	public void modify(){
		/*
		 * 解析json
		 */
		Dict dict = new Dict();
		dict.set("description", "水龙头开关");
		dict.set("id", 3);
		Dict dataDict = dictService.get(dict.getId());
		dataDict.setDescription(dict.getdescription());
		
		try {
			dictService.modify(dataDict);
		} catch (Exception e) {
			
		}
	}
	
	/**
	 * 
	 * @Title: remove  
	 * @Description: 删除字典值
	 * @author wxt.xia
	 * @throws
	 */
	public void remove(){
		int id = 3;
		try {
			dictService.remove(id);
		} catch (Exception e) {
			
		}
	}
}
